package com.sevenGroup.pawnshop.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevenGroup.pawnshop.Utils.GetJson;
import com.sevenGroup.pawnshop.Utils.GetObjectByJson;
import com.sevenGroup.pawnshop.controller.form.FirstTypeForm;
import com.sevenGroup.pawnshop.controller.info.FirstTypeInfo;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.FirstType;
import com.sevenGroup.pawnshop.dao.FirstTypeDao;
import com.sevenGroup.pawnshop.model.Goods;
import com.sevenGroup.pawnshop.model.Select;
import com.sevenGroup.pawnshop.service.FirstTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@Service
public class FirstTypeServiceImpl extends ServiceImpl<FirstTypeDao, FirstType> implements FirstTypeService {
    @Autowired
    private StringRedisTemplate srt;
    @Autowired
    private GoodsServiceImpl goodsService;
    @Override
    @Transactional
    /*
    大 类的保存和修改
     */
    public void saveFirstType(FirstTypeForm firstTypeForm) throws sevenGroupException {
        if (firstTypeForm.getFirstTypeName()==null||firstTypeForm.getFirstTypeName().isEmpty()) {
            throw  new sevenGroupException(409,"没有大类名称");
        }
        FirstType firstType = new FirstType();
        firstType.setFirstTypeName(firstTypeForm.getFirstTypeName());
        if (firstTypeForm.getFirstTypeDescribe()!=null) {
            firstType.setFirstTypeDescribe(firstTypeForm.getFirstTypeDescribe());
        }
        if (firstTypeForm.getFirstTypeId()!=null) {
            firstType.setFirstTypeId(firstTypeForm.getFirstTypeId());
        }
        if (firstTypeForm.getFirstTypeUrl()!=null&&firstTypeForm.getFirstTypeUrl().size()>0) {
            String key = GetJson.getListJson("firstTypeUrl", firstTypeForm.getFirstTypeUrl());
            firstType.setFirstTypeUrl(key);
        }
        if (firstTypeForm.getFirstTypeSort()!=null) {
            firstType.setFirstTypeSort(firstTypeForm.getFirstTypeSort());
        }
        if (firstTypeForm.getSelect()!=null&&firstTypeForm.getSelect().size()>0){
            String listJson = GetJson.getListJson("firstTypeLdentificationFigure", firstTypeForm.getSelect());
            firstType.setFirstTypeLdentificationFigure(listJson);
        }
        boolean b = saveOrUpdate(firstType);
        if (!b){
            throw new sevenGroupException(408,"添加失败");
        }
         if(firstTypeForm.getFirstTypeId()!=null) {
            Goods goods = new Goods();
            goods.setGoodFirstTypeId(firstTypeForm.getFirstTypeId());
            goods.setGoodFirstType(firstTypeForm.getFirstTypeName());
            goodsService.update(goods,new QueryWrapper<Goods>());
        }
    }
    /*
    大类的查询
     */
    @Override
    public List<FirstTypeInfo> findAll() throws sevenGroupException {
        List<FirstType> list3 = list(new QueryWrapper<FirstType>().orderByDesc("first_type_sort"));
        if (list3==null) {
            throw new sevenGroupException(409,"目前还没有大类");
        }
        List<FirstTypeInfo> list = getFirstTypeInfo(list3);
        return list;
    }
    @Transactional
    @Override
    public void del(List<Integer> id) throws sevenGroupException {
        if (id==null) {
            throw new sevenGroupException(409,"操作失败");
        }
        List<FirstType> firstTypes = listByIds(id);
        if (firstTypes==null) {
            throw new sevenGroupException(409,"该分类不存在");
        }
        removeByIds(id);
    }
    @Override
    public List<FirstTypeInfo> selectByName(String name) throws sevenGroupException {
        if (name.isEmpty()) {
            throw new sevenGroupException(409,"名称不能为空");
        }
        List<FirstType> first_type_name = list(new QueryWrapper<FirstType>().like("first_type_name", name).orderByDesc("first_type_sort"));
        if (first_type_name.size()<1){
            throw new sevenGroupException(409,"没有符合条件的分类");
        }
        List<FirstTypeInfo> list = getFirstTypeInfo(first_type_name);
        return list;
    }
    //查出来的分类进行转换的一个方法
    private static List<FirstTypeInfo> getFirstTypeInfo(List<FirstType> list){
        List<FirstTypeInfo> listInfo = new ArrayList<>();
        for (FirstType type : list) {
            List<String> list1 = null;
            if (type.getFirstTypeUrl()!=null) {
                list1 = GetObjectByJson.getList("firstTypeUrl", type.getFirstTypeUrl(),String.class);

            }
            List<Select> list2=null;
            if (type.getFirstTypeLdentificationFigure()!=null) {
               list2  = GetObjectByJson.getList("firstTypeLdentificationFigure", type.getFirstTypeLdentificationFigure(),Select.class);
            }
            FirstTypeInfo info = new FirstTypeInfo();
            info.setFirstTypeUrl(list1);
            info.setSelect(list2);
            info.setFirstTypeName(type.getFirstTypeName());
            info.setFirstTypeId(type.getFirstTypeId());
            info.setFirstTypeSort(type.getFirstTypeSort());
            info.setFirstTypeDescribe(type.getFirstTypeDescribe());
            listInfo.add(info);
        }
        return listInfo;
    }
}
